<?php
namespace App\Http\Controllers\Api;
use App\V3\V3_1\Models\FeedBackModel;
use App\V3\V3_1\Models\HelpModel;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Validator;

/**
 * 帮助
 * Class HelpController
 * @package App\Http\Controllers\Api
 */
class HelpController extends Controller
{
    /**
     * 列表
     * change 在 2021/11/30 16:28 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'parent_id' => 'integer',//父级id  0-顶级  热门问题时此参数不填
            'hot' => 'required|integer',//0-热门问题 1-其他
            'type' => 'integer',//类型：1-帮助文档 2-安全协议 3-隐私政策
            'sartTime' => 'date',//开始时间
            'endTime' => 'date',//结束时间
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $condition = [];
        //时间筛选
        if(isset($data['sartTime']) && !empty($data['sartTime']) && isset($data['endTime']) && !empty($data['endTime'])){
            $sartTime = strtotime($data['sartTime']);
            $endTime = strtotime($data['endTime']);
            if($sartTime<$endTime){
                $condition[] = ['created_at','>',$sartTime];
                $condition[] = ['created_at','<',$endTime];
            }
        }
        //标记筛选
        if(isset($data['type']) && !empty($data['type'])){
            $condition[] = ['type','=',$data['type']];
        }
        //热门
        if($data['hot']==0){
            $model = HelpModel::where($condition)->orderBy('time', 'desc')->limit(10)->get();
        }else{
            //其他
            $model = HelpModel::where('parent_id',$data['parent_id'])->orderBy('order', 'asc')->where($condition)->get();
        }
        //响应
//        foreach ($model as &$val){
//            $val['content'] = htmlspecialchars_decode($val['content']);
//        }
        return success($model);
    }

    /**
     * 详情
     * change 在 2021/12/8 15:33 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function find(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|string'//主键id 多条用逗号分隔
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = HelpModel::find($data['id']);
        return success($model);
    }

    public function getFind(Request $request)
    {
        $model = HelpModel::find($request->id);
        return $model->content;
    }

    /**
     * 安全隐私协议查询
     * change 在 2021/12/16 14:47 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function findSafePrivacy(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'type' => 'required|integer'//类型：1-帮助文档 2-安全协议 3-隐私政策
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = HelpModel::where('type',$data['type'])->orderBy('order','asc')->first();
        return success($model);
    }
    /**
     * 添加
     * change 在 2021/11/30 16:21 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'name' => 'required|string',//分类名称
            'parent_id' => 'integer',//父级id(顶级为0-协议传顶级)
            'content' => 'string',//富文本内容
            'type' => 'required|integer',//类型：1-帮助文档 2-安全协议 3-隐私政策
            'order' => 'integer',//排序：越小越靠前
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $data['created_at'] = time();
        $data['updated_at'] = time();
        HelpModel::insert($data);
        return success();
    }

    /**
     * 编辑
     * change 在 2021/11/30 16:44 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//主键id
            'name' => 'string',//分类名称
            'parent_id' => 'integer',//父级id(顶级为0)
            'content' => 'string',//富文本内容
            'type' => 'integer',//类型：1-帮助文档 2-安全协议 3-隐私政策
            'order' => 'integer',//排序：越小越靠前
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        HelpModel::where('id',$data['id'])->update($data);
        return success();
    }
    /**
     * 删除
     * change 在 2021/11/30 16:44 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|string'//主键id 多条用逗号分隔
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        HelpModel::whereIn('id',explode(',',$data['id']))->delete();
        return success();
    }

    /**
     * 打开次数统计
     * change 在 2021/11/30 16:48 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function timeCount(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        HelpModel::where('id',$data['id'])->increment('time');
        return success();
    }
}
